import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import type { DeviceApplicationStateType } from './typings'
import {
  getDeviceApplicationList,
  removeDeviceApplicationInfo,
  applyDeviceApplicationInfo,
  getExecutionLogList,
  callTestDeviceApplicationInfo
} from '~/api/monitorManagement/loopMonitorBox/deviceApplication'
import { getDataDictionaryByCode } from '~/api/global'
import { filterValueEnum } from '~/utils/util'

const deviceApplication: Module<DeviceApplicationStateType, DeviceApplicationStateType> = {
  namespaced: true,
  state: {
    deviceTaskStatusSelectList: [],
    deviceTaskStatusEnum: {},
    execTypeSelectList: [],
    execTypeEnum: {},
    runStateSelectList: [],
    runStateEnum: {}
  },
  getters: {
    deviceTaskStatusSelectList: (state) => state.deviceTaskStatusSelectList,
    deviceTaskStatusEnum: (state) => state.deviceTaskStatusEnum,
    execTypeSelectList: (state) => state.execTypeSelectList,
    execTypeEnum: (state) => state.execTypeEnum,
    runStateSelectList: (state) => state.runStateSelectList,
    runStateEnum: (state) => state.runStateEnum
  },
  mutations: {
    SET_DEVICE_TASK_STATUS_SELECT_LIST(state, payload) {
      state.deviceTaskStatusSelectList = payload
      state.deviceTaskStatusEnum = filterValueEnum(
        payload,
        'dictKey',
        'dictValue'
      ) as DeviceApplicationStateType['deviceTaskStatusEnum']
    },
    SET_EXEC_TYPE_SELECT_LIST(state, payload) {
      state.execTypeSelectList = payload
      state.execTypeEnum = filterValueEnum(
        payload,
        'dictKey',
        'dictValue'
      ) as DeviceApplicationStateType['execTypeEnum']
    },
    SET_RUN_STATE_SELECT_LIST(state, payload) {
      state.runStateSelectList = payload
      state.runStateEnum = filterValueEnum(
        payload,
        'dictKey',
        'dictValue'
      ) as DeviceApplicationStateType['runStateEnum']
    }
  },
  actions: {
    // 查询设备应用列表
    async GetDeviceApplicationList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getDeviceApplicationList<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 移出设备应用
    async RemoveDeviceApplicationInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await removeDeviceApplicationInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 应用设备应用
    async ApplyDeviceApplicationInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await applyDeviceApplicationInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 获取设备应用执行日志
    async GetExecutionLogList<S, T>(_: ActionContext<S, T>, params: T) {
      return await getExecutionLogList<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 召测设备应用
    async CallTestDeviceApplicationInfo<S, T>(_: ActionContext<S, T>, params: T) {
      return await callTestDeviceApplicationInfo<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 查询执行状态数据字典
    async GetDeviceTaskStatusSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'device_task_status' }).then(
        (res: AxiosResponse) => {
          commit('SET_DEVICE_TASK_STATUS_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    },
    // 查询执行方式数据字典
    async GetExecTypeSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'device_task_exec_type' }).then(
        (res: AxiosResponse) => {
          commit('SET_EXEC_TYPE_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    },
    // 查询执行结果数据字典
    async GetRunStateSelectList<S, T>({ commit }: ActionContext<S, T>) {
      return await getDataDictionaryByCode({ code: 'device_task_run_state' }).then(
        (res: AxiosResponse) => {
          commit('SET_RUN_STATE_SELECT_LIST', res?.data?.data || [])
          return res?.data
        }
      )
    }
  }
}

export default deviceApplication
